/**
 *
 * @author Alex & Umit
 */
public class ArrayQueue implements Queue {

    private Object[] elements;
    private int front;
    private int rear;
    private int size;
    private int capacity;

    public ArrayQueue(int cap) throws NegativeArraySizeException{
        if(cap >= 0){
            elements = new Object[cap];
            capacity = cap;
            size = 0;
            front = 0;
            rear = 0;
        }
        else throw new NegativeArraySizeException();
    }

    public boolean isFull(){
        return size == capacity;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    public Object front() throws EmptyQueueException{
        if(!isEmpty()){
            return elements[front];
        }
        else throw new EmptyQueueException();
    }

    public int size(){
        return size;
    }

    public void enqueue(Object o) throws FullQueueException{
        if(!isFull()){
            elements[rear] = o;
            rear = (rear + 1) % capacity;
            size++;
        }
        else throw new FullQueueException();
    }

    public Object dequeue() throws EmptyQueueException{
        if(!isEmpty()){
            Object res = elements[front];
            elements[front] = null;
            front = (front + 1) % capacity;
            size--;
            return res;
        }
        else throw new EmptyQueueException();
    }

    @Override
    public String toString(){
        String res = "<ArrayQueue[";
        int index = front;
        int i = 0;
        
        while(i < size){
            res += elements[index] + ",";
            index = (index + 1) % capacity;
            i++;
        }
        if(!isEmpty()){
            res = res.substring(0, res.length() - 1);
        }

        return res + "]>";
    }


}
